#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvSYS.h"

/** Define the delay count */
#define DELAYCOUNT 5000

/** Private functions */
void EINT0Callback(void);

/*************************************************************************//**
 * @brief   Init External interrupt
 * @param	None
 * @return	None
*****************************************************************************/
void GPIO_exti_Exp()
{
  	STR_UART_T param;

    UNLOCKREG();
    SYSCLK->PWRCON.XTL12M_EN = 1;
    
    /* Waiting for 12M Xtal stalble */
    SysTick->LOAD = DELAYCOUNT * 22; /* Assume the internal 22MHz RC used */
    SysTick->VAL   =  (0x00);
    SysTick->CTRL = (1 << 2) | (1);

    /* Waiting for down-count to zero */
    while((SysTick->CTRL & (1 << 16)) == 0);

	DrvGPIO_InitFunction(E_FUNC_UART0);
	
    param.u32BaudRate        = 115200;
    param.u8cDataBits        = DRVUART_DATABITS_8;
    param.u8cStopBits        = DRVUART_STOPBITS_1;
    param.u8cParity          = DRVUART_PARITY_NONE;
    param.u8cRxTriggerLevel  = DRVUART_FIFO_1BYTES;
    param.u8TimeOut        	 = 0;
    DrvUART_Open(UART_PORT0, &param);
   
    printf("\n\r");
    printf("+-----------------------------------------------------------------------+\n\r");
    printf("|                      External interrupt test                          |\n\r");
    printf("+-----------------------------------------------------------------------+\n\r");
  
    /* Configure external interrupt */
    DrvGPIO_EnableEINT0(E_IO_FALLING, E_MODE_EDGE, EINT0Callback);
}

/*********************************************************************//**
 * @brief	External interrupt handler routine
 * @param	None
 * @return	None
 **********************************************************************/
void EINT0Callback(void)
{
    printf("EINT0 Interrupt!\n");
    
}